<template>
    <div class="analyze-input-layout">
        <form :id="fid" enctype="multipart/form-data">
            <div v-if="canEdit" class="file-input-layout" data-wenk="点击选择文件" data-wenk-pos="top-left">
                
                <input class="file-input" type="file" :name="fileInputName" :id="'file'+fid" accept="image/jpg,image/jpeg,image/png,image/bmp,image/ico,image/gif" @change="fileChanged">
                <button class="btn btn-primary btn-outline" @click.prevent="openSelect" :disabled="doing">
                    <i class="fa fa-refresh fa-spin" v-show="doing"></i>
                    <i class="fa fa-upload" v-show="!doing"></i>
                </button>
                <img class="file-img" :src="src" v-show="src.length>0" @click.prevent="gallery_show=true">
            </div>
            <div v-if="!canEdit" class="file-input-layout" data-wenk="点击查看图片" data-wenk-pos="top-left">
                <img class="file-img" :src="src" v-show="src.length>0" @click.prevent="gallery_show=true">
            </div>
        </form>
        
        <Gallery :files="[path]" :base-url="base_url" :open="gallery_show" @close="gallery_show=false" :can-edit="canEdit" @delimg="delimg"></Gallery>

    </div>
</template>
<script>
import Gallery from './Gallery.vue'
import {tip,ajax} from '../tools/tools.js'
import {cfg} from '../config/config.js'

export default {
    name:'FileInput',
    components:{Gallery},
    data(){
        return {
            fid:'imgfd'+Math.random().toString().split('.')[1],

            path:'',
            base_url:cfg.baseUrl,
            src:'',

            doing:false,
            gallery_show:false
        }
    },
    props:{
        canEdit:{
            type:Boolean,
            default:true
        },
        fileInputName:{
            type:String,
            default:''
        },
        value:{
            type:String,
            default:''
        }
    },
    watch:{
        path(v,ov){
            var self = this;
            if(v.length>1){
                self.src = self.base_url + v;
            } else {
                self.src = '';
            }
        },
        value(v,ov){
            var self = this;
            if(v.length>1){
                self.path = v;
            } else {
                self.path = '';
            }
        }
    },
    filters:{

    },
    methods:{
        openSelect(){
            var self = this;
            var id = 'file'+self.fid;
            document.getElementById(id).click();
        },
        fileChanged(event){
            var self = this;
            if(event.target.value.length>0){
                self.doing = true;
                var fd = new FormData(document.getElementById(self.fid));
                ajax.uploadImg({
                    param:fd,
                    cb(data){
                        self.path = data.data.relaPathList[0];
                        self.$emit('input',self.path);
                        self.doing = false;
                    },
                    failCb(){
                        self.doing = false;
                        self.$emit('input','');
                        document.getElementById(self.fid).reset();
                    }
                });
            }
        },
        delimg(path){
            var self = this;
            self.path = '';
            self.$emit('input',self.path);
            document.getElementById(self.fid).reset();
        }
    },
    mounted(){
        var self = this;
    }
}
</script>
<style scoped>
.analyze-input-layout{
    display: inline-block;
    margin-bottom: 5px;
}

.analyze-input-layout .fa-refresh{
    display: inline-block;
    vertical-align: top;
}

.analyze-input-layout .file-input{
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    top: 0px;
    left: 0px;
    overflow: hidden;
    clip: rect(0,0,0,0);
}

.analyze-input-layout .file-img{
    max-height: 34px;
    max-width: 34px;
    cursor: pointer;
}
</style>